<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/13 0013
 * Time: 10:12
 */

namespace App\Ordersys\Repositories;

use App\Ordersys\Contracts\OrderanalysisInterface;
use App\Ordersys\Models\OrdersysOrderExpressModel;
use App\Ordersys\Models\OrdersysOrderGoodsModel;
use App\Ordersys\Models\OrdersysOrderLogModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

/**
 * 统计实现
 * Class DbOrderanalysisRepository
 * @package App\Ordersys\Repositories
 */
class DbOrderanalysisRepository implements OrderanalysisInterface
{

    /**
     *
     * @return OrdersysOrderLogModel
     */
    public function getOrdersysOrderLogModel(){
        return app()->make(OrdersysOrderLogModel::class);
    }
    /**
     * @return OrdersysOrderExpressModel
     */
    public function getOrdersysOrderExpressModel(){
        return app()->make(OrdersysOrderExpressModel::class);
    }

    /**
     * @return OrdersysOrderGoodsModel
     */
    public function getOrdersysOrderGoodsModel(){
        return app()->make(OrdersysOrderGoodsModel::class);
    }

    public function getSaleIndex($where){
        $request=Request::instance();
          return $this->getOrdersysOrderLogModel()->newQuery()->select(array(
              'sale_id', 'sale_name',
              DB::raw('SUM(all_amount) as all_amount_sum'),
             DB::raw('SUM(paid_amount) as paid_amount_sum'),
              DB::raw('count(sale_id) as sale_count')
          ))->where($where)->groupBy(["sale_id","sale_name"])->orderBy('all_amount_sum',"desc")->paginate(50);
    }
    public function getWeixinIndex($where){
        return $this->getOrdersysOrderLogModel()->newQuery()->select(array(
            'wx_no', 'wx_name',
            DB::raw('SUM(all_amount) as all_amount_sum'),
            DB::raw('SUM(paid_amount) as paid_amount_sum'),
            DB::raw('count(wx_no) as wx_count')
        ))->where($where)->groupBy(["wx_no","wx_name"])->orderBy("all_amount_sum","desc")->paginate(50);
    }
    public function getFenIndex($where){
        return $this->getOrdersysOrderLogModel()->newQuery()->select(array(
            'add_fen_type',
            DB::raw('SUM(all_amount) as all_amount_sum'),
            DB::raw('SUM(paid_amount) as paid_amount_sum'),
            DB::raw('count(add_fen_type) as wx_count')
        ))->where($where)->groupBy(["add_fen_type"])->orderBy("all_amount_sum","desc")->paginate(50);
    }
    public function getMonthIndex($where){
        return $this->getOrdersysOrderLogModel()->newQuery()->select(array(
            'add_fen_month',
            DB::raw('SUM(all_amount) as all_amount_sum'),
            DB::raw('SUM(paid_amount) as paid_amount_sum'),
            DB::raw('count(add_fen_month) as wx_count')
        ))->where($where)->groupBy(["add_fen_month"])->orderBy("all_amount_sum","desc")->paginate(10);
    }

    public function getCityIndex($where){
        return  $this->getOrdersysOrderLogModel()->newQuery()
            ->select([
                $this->getOrdersysOrderExpressModel()->table.".receiver_city",
                DB::raw('SUM(all_amount) as all_amount_sum'),
                DB::raw('SUM(paid_amount) as paid_amount_sum'),
                DB::raw('count(receiver_province) as wx_count')])
            ->leftJoin($this->getOrdersysOrderExpressModel()->table, $this->getOrdersysOrderExpressModel()->table . '.order_id', '=', $this->getOrdersysOrderLogModel()->table . '.order_id')->groupBy(["receiver_city"])->orderBy("all_amount_sum","desc")->where($where)->paginate(50);

    }

    public function getProvinceIndex($where){
        return  $this->getOrdersysOrderLogModel()->newQuery()
          ->select([
              $this->getOrdersysOrderExpressModel()->table.".receiver_province",
              DB::raw('SUM(all_amount) as all_amount_sum'),
              DB::raw('SUM(paid_amount) as paid_amount_sum'),
              DB::raw('count(receiver_province) as wx_count')])
            ->leftJoin($this->getOrdersysOrderExpressModel()->table, $this->getOrdersysOrderExpressModel()->table . '.order_id', '=', $this->getOrdersysOrderLogModel()->table . '.order_id')->groupBy(["receiver_province"])->orderBy("all_amount_sum","desc")->where($where)->paginate(50);
    }

    /**
     * 产品列表
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    //   DB::raw('SUM(goods_price*goods_num) as all_amount_sum'),
    public function getGoodsIndex($where){
         return  $this->getOrdersysOrderLogModel()->newQuery()    ->select([
            $this->getOrdersysOrderGoodsModel()->table.".goods_id",
            $this->getOrdersysOrderGoodsModel()->table.".goods_name",
            DB::raw('SUM(goods_price*goods_num) as all_amount_sum'),//DB::raw('SUM(all_amount) as all_amount_sum'),

            DB::raw('sum(goods_num) as wx_count')])->leftJoin($this->getOrdersysOrderGoodsModel()->table,$this->getOrdersysOrderGoodsModel()->table . '.order_id', '=', $this->getOrdersysOrderLogModel()->table . '.order_id')->groupBy(["goods_id","goods_name"])->orderBy("all_amount_sum","desc")->where($where)->paginate(50);
exit;

//select `ks_ordersys_order_goods`.`goods_id`, `ks_ordersys_order_goods`.`goods_name`, SUM(goods_price*goods_num) as all_amount_sum, count(goods_num) as wx_count from `ks_ordersys_order_log` left join `ks_ordersys_order_goods` on `ks_ordersys_order_goods`.`order_id` = `ks_ordersys_order_log`.`order_id` where (`option_type` = ? and `order_type` = ?) group by `goods_id`, `goods_name` order by `wx_count` desc
//select goods_id,goods_name,sum(goods_price*goods_num) AS price_amount,sum(goods_num) AS num from ks_ordersys_order_log ol left join ks_ordersys_order_goods  og on ol.order_id=og.order_id where option_type=10  and order_type=1 group by goods_id,goods_name order by num desc
   //两个总数量不一致

    }

    public function getDayIndex($where){
        $request=Request::instance();
        return $this->getOrdersysOrderLogModel()->newQuery()->select(array(
            'day_time',
            DB::raw('SUM(all_amount) as all_amount_sum'),
            DB::raw('SUM(paid_amount) as paid_amount_sum'),
            DB::raw('count(sale_id) as sale_count')
        ))->where($where)->groupBy(["day_time"])->orderBy("day_time","desc")->paginate(50);
    }

    public function getSumSaleMoney($where){
     return $this->getOrdersysOrderLogModel()->newQuery()->where($where)->sum("all_amount");
    }
    public function getCitySumSaleMoney($where){
        return $this->getOrdersysOrderLogModel()->newQuery()
            ->leftJoin($this->getOrdersysOrderExpressModel()->table, $this->getOrdersysOrderExpressModel()->table . '.order_id', '=', $this->getOrdersysOrderLogModel()->table . '.order_id')->where($where)->sum("all_amount");
    }
    public function getAddFenType($where){
        //统计三个
       return  $this->getOrdersysOrderLogModel()->newQuery()->select(array(  DB::raw('SUM(all_amount) as new_fun_amount'),
            DB::raw('count(all_amount) as new_fen_count')))->where($where)->first();

    }



}
